package vitaliy.bondar.diamondcave;

import java.util.ArrayList;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

interface OnCurrentChangedListener {
    void onCurrentChanged(String value);
}

public class TextSelector extends Entity implements ITouchArea
{
	private int m_current;
	private final Sprite m_left;
	private final Sprite m_right;
	private final Text m_label;
	private ArrayList < String > m_list;
	private boolean m_blocked;
	
	private final OnCurrentChangedListener m_listener;
	
	public TextSelector(final float x, final float y, IFont font,
	        final VertexBufferObjectManager vbom, 
			final OnCurrentChangedListener listener) 
	{
		m_listener = listener;
		m_blocked = false;
		m_current = -1;
		
		m_left = new Sprite(x, y, ResourcesManager.getInstance().button_left_region, vbom);
		m_right = new Sprite(0, y, ResourcesManager.getInstance().button_right_region, vbom);
		m_label = new Text(0, y, font, "   ", vbom);
		
		this.attachChild(m_left);
		this.attachChild(m_label);
		this.attachChild(m_right);
		
	}
	
	public void setCurrentText(String text)
	{
	    if (!m_list.contains(text))
	    {
	        m_current = -1;
	        return;
	    }
	    this.setCurrentIndex(m_list.indexOf(text));
	}
	
	public void setSize(float width, float height)
	{
	    m_left.setSize(height, height);
	    m_right.setSize(height, height);
	    float offsetX = width / 2 - m_label.getWidth() / 2;
	    m_label.setX(m_left.getX() + offsetX);
	    m_right.setX(m_left.getX() + width - height);
	}
	
	public void setList(ArrayList< String > list)
	{
	    m_list = list;
	}
	
	public void setCurrentIndex(int index)
	{
	    if (index < 0 || index >= m_list.size())
        {
            m_current = -1;
            return;
        }
	    if (m_current == index) return;
	    
	    m_current = index;
	    String text = m_list.get(index);
	    m_label.setText(m_list.get(index));
	    if (!m_blocked)
	    {
	    	m_listener.onCurrentChanged(text);
	    }
	}
	
    @Override
    public boolean contains(float x, float y)
    {
        return m_left.contains(x, y) || m_right.contains(x, y);
    }

    @Override
    public boolean onAreaTouched(TouchEvent event, float x, float y)
    {
        if (event.isActionDown())
        {
            if (m_left.contains(x, y))
            {
                setCurrentIndex(m_current > 0 ? m_current - 1 : m_list.size() - 1);
            }
            else if (m_right.contains(x, y))
            {
                setCurrentIndex(m_current < m_list.size() - 1 ? m_current + 1 : 0);
            }
        }
        return true;
    }
    
    public void blockSignals(boolean block)
    {
    	m_blocked = block;
    }
    
    public final boolean signalsBlocked()
    {
    	return m_blocked;
    }
}
